﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ExtAspNet;
using System.Data;
using Ylh.BLL;

public partial class Module_CustManger : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button9.OnClientClick = Window2.GetSaveStateReference(SQLwhere.ClientID)+ Window2.GetShowReference("CustSearchCondition.aspx", "客户查找条件");

        if (!IsPostBack)
        {
            BinbGvlist();
        }
        //如果入口是保护客户的话,隐藏新增按钮
        if (GetCustType() == "ProtectCust")
        {
            BtnAdd.Hidden = true;
        }
        //如果入口是已签客户的话,隐藏整个工具栏
        if (GetCustType() == "SigningCust")
        {
            //tool1.Hidden = true;
            BtnAdd.Hidden = true;
            Button2.Hidden = true;
        }

    }

    private string GetCustType()
    {
        switch (Request.QueryString["CustType"].ToString())
        {
            case "ProtectCust":
                return "ProtectCust";
            case "IntentCust":
                return "IntentCust";
            case "SigningCust":
                return "SigningCust";
            default:
                return "";
        }
    }


    private void BinbGvlist()
    {
        string SQLwhere = string.Empty;

        if (ViewState["SQLwhere"]!=null && !string.IsNullOrEmpty(ViewState["SQLwhere"].ToString()))
        {
            SQLwhere = ViewState["SQLwhere"].ToString();
        }


        int pc = Grid1.RecordCount;
        DataTable dt;
        CustBLL blls = new CustBLL(DbConnectionStr);
        if (GetCustType() == "ProtectCust")
        {
            dt = blls.GetCustList(StaffID.ToString(), true, Grid1.PageIndex + 1, Grid1.PageSize, ref pc, SQLwhere);
        }
        else if (GetCustType() == "IntentCust")
        {

            dt = blls.GetCustList(StaffID.ToString(), false, Grid1.PageIndex + 1, Grid1.PageSize, ref pc, SQLwhere);
           
        }
        else if (GetCustType() == "SigningCust")
        {
            dt = blls.GetCustList(StaffID.ToString(), true, "", Grid1.PageIndex + 1, Grid1.PageSize, ref pc, SQLwhere);
        }
        else
        {
            dt = blls.GetCustList(StaffID.ToString(), null, Grid1.PageIndex + 1, Grid1.PageSize, ref pc, SQLwhere);
        }

        Grid1.RecordCount = pc;
        Grid1.DataSource = dt;
        Grid1.DataBind();

    }

    protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
    {
        System.Web.UI.WebControls.LinkButton lbf = (System.Web.UI.WebControls.LinkButton)Grid1.Rows[e.RowIndex].FindControl("lbf");
        string str = Grid1.DataKeys[e.RowIndex][0].ToString();

        if (GetCustType() == "ProtectCust")
        {
            lbf.OnClientClick = "AddTab('Module/CustManger/CustDetailed.aspx?CustType=ProtectCust&CustID=" + str + "', 'Cust_CustDetailed', '客户详细信息')";
        }
        else if (GetCustType() == "IntentCust")
        {
            lbf.OnClientClick = "AddTab('Module/CustManger/CustDetailed.aspx?CustType=IntentCust&CustID=" + str + "', 'Cust_CustDetailed', '客户详细信息')";
        }
        else if (GetCustType() == "SigningCust")
        {
            lbf.OnClientClick = "AddTab('Module/CustManger/CustDetailed.aspx?CustType=SigningCust&CustID=" + str + "', 'Cust_CustDetailed', '客户详细信息')";
        }

        System.Web.UI.WebControls.LinkButton lbfOption = (System.Web.UI.WebControls.LinkButton)Grid1.Rows[e.RowIndex].FindControl("lbfOption");

        lbfOption.OnClientClick = Window4.GetShowReference("CustOption.aspx?CustID=" + str, "高层意见");

        //隔行变色
        DataRowView row = e.DataItem as DataRowView;
        if (row != null)
        {
            string CustName = row["CustName"].ToString();

            if (CustName.Contains("先生") || CustName.Contains("小姐"))
            {
                highlightRows.Text += e.RowIndex.ToString() + ",";
            }
        }
    }
    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        if (GetCustType() == "ProtectCust")
        {
            PageContext.RegisterStartupScript(Window3.GetShowReference("AddProtectCust.aspx?CustType=ProtectCust", "新增客户"));
        }
        else if (GetCustType() == "IntentCust")
        {
            PageContext.RegisterStartupScript(Window3.GetShowReference("AddIntentCustaspx.aspx?CustType=IntentCust", "新增客户"));
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {

        List<string> li = base.GetCheckID(Grid1);


        if (li.Count == 0)
        {
            Alert.Show("请先选择需要删除客户");
            return;
        }

        Ylh.BLL.CustBLL cust = new CustBLL(DbConnectionStr);
        cust.deleteCust(li);


        BinbGvlist();

        Alert.Show("删除成功");
    }


    protected void Window3_close(object sender, WindowCloseEventArgs e)
    {
        BinbGvlist();
    }


    protected void Window2_close(object sender, WindowCloseEventArgs e)
    {
        ViewState["SQLwhere"] = SQLwhere.Text;
        BinbGvlist();
    }


    /// <summary>
    /// 刷新按钮,显示所有的客户信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        ViewState["SQLwhere"] = string.Empty;
        BinbGvlist();
    }

    protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
    {
        //如果不加上这句,各行变色的效果会带到下一页!
        highlightRows.Text = "";

        Grid1.PageIndex = e.NewPageIndex;
        BinbGvlist();
        //
    } 
}